package com.ruoyi.warning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.warning.domain.entity.Area;
import com.ruoyi.warning.domain.query.AreaQuery;
import com.ruoyi.warning.domain.vo.AreaVO;
import com.ruoyi.warning.mapper.AreaMapper;
import com.ruoyi.warning.service.HikApiService;
import com.ruoyi.warning.service.IAreaService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 区域表 服务实现类
 * </p>
 *
 * @author chenhc
 * @since 2025-02-25
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {
    @Resource
    private HikApiService hikApiService;
    @Resource
    private AreaMapper areaMapper;
    @Override
    public TableDataInfo getAreaPage(AreaQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        LambdaQueryWrapper<Area> wrapper = new QueryWrapper<Area>().lambda()
                .orderByDesc(Area::getSort);
        List<Area> list = this.list(wrapper);
        TableDataInfo tableDataInfo = new TableDataInfo(list, page.getTotal());
        return tableDataInfo;
    }

    @Override
    public List<Area> getAreaList(AreaQuery query) {
        LambdaQueryWrapper<Area> wrapper = new QueryWrapper<Area>().lambda()
                .orderByDesc(Area::getSort);
        return this.list(wrapper);
    }

    @Override
    public Object refresh() {
        areaMapper.removeAll();
        this.saveBatch(hikApiService.nodesByParams());
        return null;
    }

    @Override
    public Object getAreaTree(AreaQuery query) {
        //this.refresh();
        PageHelper.clearPage();
        List<AreaVO> list = areaMapper.getAreaList();
        // 用于快速查找每个节点
        Map<String, AreaVO> nodeMap = new HashMap<>();
        System.out.println("+++++++++++++++++++++++++++");
        System.out.println(list.size());
//        AreaVO root = new AreaVO();
        for (AreaVO node : list) {
            nodeMap.put(node.getAreaCode(), node);
//            if (node.getParentIndexCode().equals("-1")) {
//                root = node;
//            }
        }
        // 构建树结构

        List<AreaVO> result = new ArrayList<>();
        for (AreaVO node : list) {
            String parentId = node.getParentIndexCode();
            if (!parentId.equals("-1")) {
                AreaVO parentNode = nodeMap.get(parentId);
                if (parentNode != null) {
                    parentNode.addChild(node);
//                    if (parentNode.getAreaList() == null) {
//                        List<AreaVO> areaList = new ArrayList<>();
//                        parentNode.setAreaList(areaList);
//                    }
//                    parentNode.getAreaList().add(node);
//                    nodeMap.put(parentNode.getAreaCode(), parentNode);
                }
            }else {
                result.add(node);
            }
        }
        return result;
    }

    @Override
    public Object updateUsed(String id) {
        Area area = this.getById(id);
        if(area.getIsUsed().equals("0")){
            area.setIsUsed("1");
        }else {
            area.setIsUsed("0");
        }
        this.updateById(area);
        return null;
    }
}
